DragStartHelper

DragStartHelper is a utility class for implementing drag and drop support.

It detects gestures commonly used to start drag (long click for any input source, click and drag for mouse).

It also keeps track of the screen location where the drag started, and helps determining the hot spot position for a drag shadow.

Implement DragStartHelper.OnDragStartListener to start the drag operation:

DragStartHelper.OnDragStartListener listener = new DragStartHelper.OnDragStartListener {
    protected void onDragStart(View view, DragStartHelper helper) {
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view) {
            public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
                super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
                helper.getTouchPosition(shadowTouchPoint);
            }
        };
        view.startDrag(mClipData, shadowBuilder, mLocalState, mDragFlags);
    }
};
mDragStartHelper = new DragStartHelper(mDraggableView, listener);
Once created, DragStartHelper can be attached to a view (this will replace existing long click and touch listeners):
mDragStartHelper.attach();
It may also be used in combination with existing listeners:
public boolean onTouch(View view, MotionEvent event) {
    if (mDragStartHelper.onTouch(view, event)) {
        return true;
    }
    return handleTouchEvent(view, event);
}
public boolean onLongClick(View view) {
    if (mDragStartHelper.onLongClick(view)) {
        return true;
    }
    return handleLongClickEvent(view);
}

Constructors

Link copied to clipboard
constructor(@NonNull view: View, @NonNull listener: DragStartHelper.OnDragStartListener)
Create a DragStartHelper associated with the specified view.

Types

Link copied to clipboard
Interface definition for a callback to be invoked when a drag start gesture is detected.

Functions

Link copied to clipboard
open fun attach()
Attach the helper to the view.
Link copied to clipboard
open fun detach()
Detach the helper from the view.
Link copied to clipboard
open fun getTouchPosition(@NonNull point: Point)
Compute the position of the touch event that started the drag operation.
Link copied to clipboard
Handle a long click event.
Link copied to clipboard
open fun onTouch(@NonNull v: View, @NonNull event: MotionEvent): Boolean
Handle a touch event.